package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.mapper.MemberMapper;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

@Service(interfaceClass = MemberService.class)
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberMapper memberMapper;

    @Override
    public Member findByTelephone(String telephone) {
        return memberMapper.findByTelephone(telephone);
    }

    @Override
    public void add(Member member) {
        memberMapper.add(member);
    }

    @Override
    public Map getMemberReport(String startDate0, String endDate0) throws Exception {



//        如果没有选中开始时间，根据今日时间往前推12个月，前端初始结束时间为今日
        Date startDate = null;
        if (startDate0 == null || startDate0.equals("")) {
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.MONTH, -12);//日历对象的当前事件往前推12个月
            startDate = calendar.getTime();
        } else {
            startDate = DateUtils.parseString2Date(startDate0);
        }
        Date endDate = DateUtils.parseString2Date(endDate0);
//        循环月数
        List<String> memberMonths = DateUtils.getMonthBetween(
                DateUtils.parseDate2String(startDate, "yyyy-MM"),
                DateUtils.parseDate2String(endDate, "yyyy-MM"),
                "yyyy-MM");
        List<Integer> memberCounts = new ArrayList<>();
//        循环遍历。
        for (String month : memberMonths) {
            String dateBegin = month + "-01";
            String dateEnd = month + "-31";
            Integer memberCount = memberMapper.getMemberReport(dateBegin, dateEnd);
            memberCounts.add(memberCount);
        }

        //创建一个map，将月份的集合以及每个月会员数量的集合存入其中
        Map<String, List> map = new HashMap<>();
        map.put("memberMonths", memberMonths);
        map.put("memberCounts", memberCounts);

        return map;
    }

    public static void main(String[] args) throws Exception {
        //实例化日历对象
        Calendar calendar = Calendar.getInstance();
        //将日历对象的当前时间往前推12个月
        calendar.add(Calendar.MONTH,-12);

        //循环遍历，获取过去的十二个月
        for (int i = 0; i < 12; i++) {
            Date time = calendar.getTime();
            String date = DateUtils.parseDate2String(time,"yyyy-MM");
            System.out.println(date);

            calendar.add(Calendar.MONTH,1);
        }
    }
    /**
     * 获取会员性别统计信息
     * */
    @Override
    public Map getMemberSexs() {
        List<Map> list = memberMapper.getMemberSexs();
        List<String> names = new ArrayList<>();
        for (Map map : list) {
            String name = (String) map.get("sex");
            names.add(name);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("getSexs",names);
        map.put("getCounts",list);
        return map;
    }
    /**
     * 获取会员年龄统计信息
     * */
    @Override
    public Map getMemberAges() {
        List<Map> list =memberMapper.getMemberAges();
        List<String> names = new ArrayList<>();
        for (Map map : list) {
            String name = (String) map.get("age");
            names.add(name);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("getAges",names);
        map.put("getCounts",list);
        return map;
    }
}
